﻿
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


    /// <summary>
    /// 帧率控制器，控制战斗场景中所有实体对象的帧率刷新:Update,FixUpdate
    /// </summary>
    public class Updater : MonoBehaviour
    {
        private static bool Paused = false;
        private static bool bCreate;

        private static float _unscaleAliveTime;

        private static float _unscaledeltatime;

        private static Updater updater;

        public static float AliveTime => Time.time;

        public static float delta => Time.deltaTime;

        public static float deltaIgnoreTime => Time.unscaledDeltaTime;

        public static float unscaleAliveTime => Time.unscaledTime;


        private static Updater _instance;

        private static object _lock = new object();

        private static bool applicationIsQuitting = false;

        private LinkedList<Action<float>> onUpdate = new LinkedList<Action<float>>();
        private LinkedList<Action<float>> onUpdateIgnoreTime = new LinkedList<Action<float>>();
        private LinkedList<Action> onLateUpdate = new LinkedList<Action>();
        private LinkedList<Action> onFixedUpdate = new LinkedList<Action>();



        public static Updater Instance
        {
            get
            {
                if (applicationIsQuitting)
                {
                    return null;
                }
                // lock (_lock)
                {
                    if (_instance == null)
                    {
                        _instance = (Updater)FindObjectOfType(typeof(Updater));
                        if (_instance != null)
                        {
                            bCreate = true;
                            return _instance;
                        }
                        else
                        {
                            GameObject gameObject = new GameObject();
                            _instance = gameObject.AddComponent<Updater>();
                            gameObject.name = ("(singleton) Updater");
                            DontDestroyOnLoad(gameObject);
                            bCreate = true;
                        }
                    }
                    return _instance;
                }
            }
        }
        public static void Init()
        {
            applicationIsQuitting = false;
        }

        protected virtual void OnApplicationQuit()
        {
            applicationIsQuitting = true;
            if (_instance == null) return;
            Destroy(_instance.gameObject);
            _instance = null;
        }

        public void OnDestroy()
        {
            _instance = null;
        }

        public static void Dispose()
        {
            if (updater != null)
            {
                applicationIsQuitting = true;
                updater.onUpdate.Clear(); updater.onUpdate = null;
                updater.onUpdateIgnoreTime.Clear(); updater.onUpdateIgnoreTime = null;
                updater.onLateUpdate.Clear(); updater.onLateUpdate = null;
                updater.onFixedUpdate.Clear(); updater.onFixedUpdate = null;
                Destroy(updater.gameObject);
                updater = null;
                bCreate = false;

            }
        }

        LinkedListNode<Action<float>> next;
        private void Update()
        {
            //  UnityEngine.Profiling.Profiler.BeginSample("Updater.Update");
            if (!Paused)
            {
                _unscaledeltatime = Time.unscaledDeltaTime;
                _unscaleAliveTime += _unscaledeltatime;
            }
            if (this.onUpdateIgnoreTime != null)
            {
                for (var node = onUpdateIgnoreTime.First; node != null; node = next)
                {
                    next = node.Next;
                    var e = node.Value;
                    e?.Invoke(deltaIgnoreTime);
                }
            }
            if (this.onUpdate != null && !Paused)
            {
                for (var node = onUpdate.First; node != null; node = next)
                {
                    next = node.Next;
                    var e = node.Value;
                    e?.Invoke(delta);
                }
            }
            //  UnityEngine.Profiling.Profiler.EndSample();
        }


        LinkedListNode<Action> nextLate;
        private void LateUpdate()
        {
            //  UnityEngine.Profiling.Profiler.BeginSample("Updater.LateUpdate");
            if (this.onLateUpdate != null && !Paused)
            {
                for (var node = onLateUpdate.First; node != null; node = nextLate)
                {
                    nextLate = node.Next;
                    var e = node.Value;
                    e?.Invoke();
                }
            }
            //   UnityEngine.Profiling.Profiler.EndSample();
        }

        LinkedListNode<Action> nextFixed;
        private void FixedUpdate()
        {
            // UnityEngine.Profiling.Profiler.BeginSample("Updater.FixedUpdate");
            if (this.onFixedUpdate != null && !Paused)
            {
                for (var node = onFixedUpdate.First; node != null; node = nextFixed)
                {
                    nextFixed = node.Next;
                    var e = node.Value;
                    e?.Invoke();
                }
            }
            // UnityEngine.Profiling.Profiler.EndSample();
        }

        public static Updater Get(GameObject go)
        {
            return go.GetComponent<Updater>() ?? go.AddComponent<Updater>();
        }

        public static Updater GetUpdater()
        {
            if (updater == null)
            {
                updater = Instance;
            }
            return updater;
        }
        public static void AddUpdate(string name ,Action<float> func, bool IgnoreTimeScale = false)
        {
            AddUpdate(func,IgnoreTimeScale);
        }
        public static LinkedListNode<Action<float>> AddUpdate(Action<float> func, bool IgnoreTimeScale = false)
        {

            Updater updater = GetUpdater();
            LinkedListNode<Action<float>> node = null;
            if (updater == null) return node;

            // UnityEngine.Profiling.Profiler.BeginSample("AddUpdate");

            if (!IgnoreTimeScale)
            {
                node = updater.onUpdate.AddLast(func);

            }
            else
            {
                node = updater.onUpdateIgnoreTime.AddLast(func);
            }

            //  UnityEngine.Profiling.Profiler.EndSample();
            return node;

        }
    public static void RemoveUpdate(string name,Action<float> func, bool IgnoreTimeScale = false)
    {
        RemoveUpdate(func, IgnoreTimeScale);
    }
        public static void RemoveUpdate(Action<float> func, bool IgnoreTimeScale = false)
        {

            Updater updater = GetUpdater();
            if (updater == null) return;

            // UnityEngine.Profiling.Profiler.BeginSample("RemoveUpdate");

            LinkedListNode<Action<float>> next;
            if (!IgnoreTimeScale)
            {
                for (var node = updater.onUpdate.First; node != null; node = next)
                {
                    next = node.Next;
                    var e = node.Value;
                    if (func == e)
                    {
                        updater.onUpdate.Remove(e);
                        break;
                    }
                }
            }
            else
            {
                for (var node = updater.onUpdateIgnoreTime.First; node != null; node = next)
                {
                    next = node.Next;
                    var e = node.Value;
                    if (func == e)
                    {
                        updater.onUpdateIgnoreTime.Remove(e);
                        break;
                    }
                }
            }

            // UnityEngine.Profiling.Profiler.EndSample();
        }

        public static void RemoveUpdate(LinkedListNode<Action<float>> node, bool IgnoreTimeScale = false)
        {

            Updater updater = GetUpdater();
            if (updater == null || node == null) return;

            if (node.Value == null)
            {
                if (IgnoreTimeScale)
                    updater.onUpdateIgnoreTime.Remove(node);
                else
                    updater.onUpdate.Remove(node);
                return;
            }

            //   UnityEngine.Profiling.Profiler.BeginSample("RemoveUpdate");

            if (IgnoreTimeScale)
                updater.onUpdateIgnoreTime.Remove(node);
            else
                updater.onUpdate.Remove(node);

            //  UnityEngine.Profiling.Profiler.EndSample();
        }

        public static LinkedListNode<Action> AddLateUpdate(Action func)
        {
            Updater updater = GetUpdater();
            LinkedListNode<Action> node = null;
            if (updater == null) return node;

            //UnityEngine.Profiling.Profiler.BeginSample("AddLateUpdate");

            node = updater.onLateUpdate.AddLast(func);

            //UnityEngine.Profiling.Profiler.EndSample();
            return node;
        }

        //public static void RemoveLateUpdate(Action func)
        //{
        //    Updater updater = GetUpdater();
        //    if (updater == null) return;

        //    UnityEngine.Profiling.Profiler.BeginSample("RemoveLateUpdate");
        //    LinkedListNode<Action> next;
        //    for (var node = updater.onLateUpdate.First; node != null; node = next)
        //    {
        //        next = node.Next;
        //        var e = node.Value;
        //        if (func == e)
        //        {
        //            updater.onLateUpdate.Remove(e);
        //            break;
        //        }
        //    }
        //    UnityEngine.Profiling.Profiler.EndSample();
        //}

        public static void RemoveLateUpdate(LinkedListNode<Action> node)
        {
            Updater updater = GetUpdater();
            if (updater == null || node == null) return;
            if (node.Value == null)
            {
                updater.onLateUpdate.Remove(node);
                return;
            }

            //UnityEngine.Profiling.Profiler.BeginSample("RemoveLateUpdate");

            updater.onLateUpdate.Remove(node);

            // UnityEngine.Profiling.Profiler.EndSample();
        }

        public static LinkedListNode<Action> AddFixedUpdate(Action func)
        {


            Updater updater = GetUpdater();
            LinkedListNode<Action> node = null;
            if (updater == null) return node;
            // UnityEngine.Profiling.Profiler.BeginSample("AddFixedUpdate");
            node = updater.onFixedUpdate.AddLast(func);

            // UnityEngine.Profiling.Profiler.EndSample();
            return node;
        }

        //public static void RemoveFixedUpdate(Action func)
        //{
        //    UnityEngine.Profiling.Profiler.BeginSample("RemoveFixedUpdate");

        //    Updater updater = GetUpdater();
        //    if (updater == null) return;

        //    LinkedListNode<Action> next;
        //    for (var node = updater.onFixedUpdate.First; node != null; node = next)
        //    {
        //        next = node.Next;
        //        var e = node.Value;
        //        if (func == e)
        //        {
        //            updater.onFixedUpdate.Remove(e);
        //            break;
        //        }
        //    }
        //    UnityEngine.Profiling.Profiler.EndSample();
        //}

        public static void RemoveFixedUpdate(LinkedListNode<Action> node)
        {


            Updater updater = GetUpdater();
            if (updater == null || node == null) return;
            if (node.Value == null)
            {
                updater.onFixedUpdate.Remove(node);
                return;
            }

            // UnityEngine.Profiling.Profiler.BeginSample("RemoveFixedUpdate");
            updater.onFixedUpdate.Remove(node);

            // UnityEngine.Profiling.Profiler.EndSample();
        }

    }

